import sys
#import torchaudio
import wave

wav_file_in = sys.argv[1]
wav_file_out = sys.argv[2]

max_length = 10
min_length = 0

with open(wav_file_in, 'r', encoding='utf-8') as f:
    with open(wav_file_out, 'w', encoding='utf-8') as w:
        for line in f:
            parts = line.strip().split()
            path = parts[1]
            ob = wave.open(path, 'rb')
            params = ob.getparams()
            nchannels, samplewidth, rate, nframes = params[:4]
            str_data = ob.readframes(nframes)
            wavform = np.fromstring(str_data, dtype=np.short())
            wavform.shape = -1, 1
            wavform = wavform.T
            wavform = torch.tensor(wavform)
          #  wavform, rate = torchaudio.load(path)

            dur = wavform.size(0) / rate
            if  dur > max_length or dur < min_length:
                print('Skip %s' % path)
            else:
                w.write(line)
